package xml.tests;

import junit.framework.TestCase;
import xml.Xml;

public class XmlTest extends TestCase {

	private static final boolean debug = true;

	public static final String toStr(Object obj) {
		if (obj==null) {
			return "null";
		} else {
			return obj.toString();
		}
	}

	public static final boolean myEquals(Object A, Object B) {
		if (A==null) {
			return B==null;
		} else {
			return (B!=null && A.equals(B));
		}
	}

	public static final void println(String S)		{	if (debug) System.out.println(S);				}
	public static final void printBreak()			{	println("===============================");		}
	public static final void title(String S)		{	printBreak();	println(S);		printBreak();	}
	public static final void smallTitle(String S)	{	println("===== " + S + " =====");				}

	public static final void strTest(String S1, String S2) {
		boolean result;
		smallTitle("String1");
		println(toStr(S1));
		smallTitle("String2");
		println(toStr(S2));
		smallTitle("Results");
		result = myEquals(S1,S2);
		assertTrue(result);
	}

	public static final String	SOB = "<";
	public static final String	SCB = ">";
	public static final String	EOB = "</";
	public static final String	ECB = ">";

	public class Prim {

		public Prim	myPrim		= null;


		public boolean 	myPublicBoolean 	= true;
		public byte		myPublicByte		= (byte)	0x01;
		public short	myPublicShort		= (short)	0x0001;
		public int		myPublicInt			= (int)		0x00000001;
		public long		myPublicLong		= (long)	0x0000000000000001;
		public char		myPublicChar		= 'X';
		public float	myPublicFloat		= 99.99F;
		public double	myPublicDouble		= 999999.99D;

		public Prim() {		}
	}

	public static final void setXmlBraces() {
		title("setXmlBraces");
		Xml.setPrintNice(false);
		Xml.setStartOpenBrace(SOB);
		Xml.setStartCloseBrace(SCB);
		Xml.setEndingOpenBrace(EOB);
		Xml.setEndingCloseBrace(ECB);
	}

	////////////////////////////////////////////////////
	//
	// TEST CASES
	//
	////////////////////////////////////////////////////

	public void testGetXml() {
		title("testGetXml");
		setXmlBraces();
		String	xml		= "</STU><STU>Hello World</STU><STU>";
		String	xml2	= "<STU>Hello World</STU>";
		String	xml3	= Xml.getXml("STU", xml);
		assertTrue(xml2.equals(xml3));
	}

	public void testGetTag() {
		title("testGetTag");
		setXmlBraces();
		String	xml		= "xxxxx</STU>xx<STU><STU>aaa</STU></STU>xxx";
		String	xml2	= Xml.getTag("STU", xml);
		String	xml3	= "<STU>aaa</STU>";
		strTest(xml2,xml3);
	}

	public void testFindStartTag() {
		title("testFindStartTag");
		setXmlBraces();
		String	xml = "xxxxx<STU>xxxxx";
		int		idx = Xml.findStartTag("STU", xml);

		println("idx = " + idx);
		assertTrue(idx==5);
	}

	public void testFindEndTag() {
		title("testFindEndTag");
		setXmlBraces();
		String	xml = "xxxxx</STU>xx<STU>xxxxx</STU>xxxxx<STU>xxx";
		int		idx = Xml.findEndTag("STU", xml);

		println("idx = " + Integer.toString(idx));
		assertTrue(idx==23);
	}

	public void testStartTag() {
		title("testStartTag");
		setXmlBraces();
		String	xml = Xml.startTag("STU");
		String	xml2	= "<STU>";
		strTest(xml,xml2);

	}

	public void testEndTag() {
		title("testEndTag");
		setXmlBraces();
		String	xml = Xml.endTag("STU");
		String	xml2	= "</STU>";
		strTest(xml,xml2);

	}

	public void testWriteStringString() {
		title("testWriteStringString");
		setXmlBraces();
		String	xml = Xml.write("STU", "Hello World");
		String	xml2	= "<STU>Hello World</STU>";
		strTest(xml,xml2);
	}

	public void testWriteStringInt() {
		title("testWriteStringInt");
		setXmlBraces();
		String	xml = Xml.write("STU", -1);
		String	xml2	= "<STU>-1</STU>";
		strTest(xml,xml2);
	}

	public void testWriteStringByte() {
		title("testWriteStringByte");
		setXmlBraces();
		String	xml = Xml.write("STU", (byte) 0x0F);
		String	xml2	= "<STU>15</STU>";
		strTest(xml,xml2);
	}

	public void testWriteStringShort() {
		title("testWriteStringShort");
		setXmlBraces();
		String	xml = Xml.write("STU", (short) 0x00FF);
		String	xml2	= "<STU>255</STU>";
		strTest(xml,xml2);
	}

	public void testWriteStringChar() {
		title("testWriteStringChar");
		setXmlBraces();
		String	xml = Xml.write("STU", 'X');
		String	xml2	= "<STU>X</STU>";
		strTest(xml,xml2);
	}

	public void testWriteStringFloat() {
		title("testWriteStringFloat");
		setXmlBraces();
		String	xml = Xml.write("STU", 9.99F);
		String	xml2	= "<STU>9.99</STU>";
		strTest(xml,xml2);
	}

	public void testWriteStringDouble() {
		title("testWriteStringDouble");
		setXmlBraces();
		String	xml = Xml.write("STU", 999.999D);
		String	xml2	= "<STU>999.999</STU>";
		strTest(xml,xml2);
	}

	public void testWriteStringLong() {
		title("testWriteStringLong");
		setXmlBraces();
		String	xml = Xml.write("STU", 999L);
		String	xml2	= "<STU>999</STU>";
		strTest(xml,xml2);
	}

	public void testWriteStringBoolean() {
		title("testWriteStringBoolean");
		setXmlBraces();
		String	xml = Xml.write("STU", true);
		String	xml2	= "<STU>true</STU>";
		strTest(xml,xml2);
	}

	public void testWriteStringInteger() {
		title("testWriteStringInteger");
		setXmlBraces();
		String	xml = Xml.write("STU", -999);
		String	xml2	= "<STU>-999</STU>";
		strTest(xml,xml2);
	}

	public void testWriteStringByte1() {
		title("testWriteStringByte1");
		setXmlBraces();
		String	xml = Xml.write("STU", new Byte((byte) 0x0F));
		String	xml2	= "<STU>15</STU>";
		strTest(xml,xml2);
	}

	public void testWriteStringShort1() {
		title("testWriteStringShort1");
		setXmlBraces();
		String	xml = Xml.write("STU", new Short((short) 0x00FF));
		String	xml2	= "<STU>255</STU>";
		strTest(xml,xml2);
	}

	public void testWriteStringCharacter() {
		title("testWriteStringCharacter");
		setXmlBraces();
		String	xml = Xml.write("STU", new Character('X'));
		String	xml2	= "<STU>X</STU>";
		strTest(xml,xml2);
	}

	public void testWriteStringFloat1() {
		title("testWriteStringFloat1");
		setXmlBraces();
		String	xml = Xml.write("STU", new Float(99999.9f));
		String	xml2	= "<STU>99999.9</STU>";
		strTest(xml,xml2);
	}

	public void testWriteStringDouble1() {
		title("testWriteStringDouble1");
		setXmlBraces();
		String	xml = Xml.write("STU", new Double(999999.99d));
		String	xml2	= "<STU>999999.99</STU>";
		strTest(xml,xml2);
	}

	public void testWriteStringLong1() {
		title("testWriteStringLong1");
		setXmlBraces();
		String	xml = Xml.write("STU", new Long(9999999999L));
		String	xml2	= "<STU>9999999999</STU>";
		strTest(xml,xml2);
	}

	public void testWriteStringBoolean1() {
		title("testWriteStringBoolean1");
		setXmlBraces();
		String	xml = Xml.write("STU", new Boolean(true));
		String	xml2	= "<STU>true</STU>";
		strTest(xml,xml2);
	}

	public void testWriteStringObject() {
		title("testWriteStringObject");
		setXmlBraces();
		String	xml = Xml.write("STU", new Short((short) 0x00FF));
		String	xml2	= "<STU>255</STU>";
		strTest(xml,xml2);
	}

	public void testSetXmlBraces() {
		title("testSetXmlBraces");
		setXmlBraces();
		assertTrue(Xml.getStartOpenBrace().equals(SOB));
		assertTrue(Xml.getEndingOpenBrace().equals(EOB));
		assertTrue(Xml.getStartCloseBrace().equals(SCB));
		assertTrue(Xml.getEndingCloseBrace().equals(ECB));
	}

	public void testGetStartOpenBrace() {
		title("testGetStartOpenBrace");
		setXmlBraces();
		Xml.setStartOpenBrace("XXX");
		assertTrue(Xml.getStartOpenBrace().equals("XXX"));
	}

	public void testGetStartCloseBrace() {
		title("testGetStartCloseBrace");
		setXmlBraces();
		Xml.setStartCloseBrace("XXX");
		assertTrue(Xml.getStartCloseBrace().equals("XXX"));
	}

	public void testGetEndingOpenBrace() {
		title("testGetEndingOpenBrace");
		setXmlBraces();
		Xml.setEndingOpenBrace("XXX");
		assertTrue(Xml.getEndingOpenBrace().equals("XXX"));
	}

	public void testGetEndingCloseBrace() {
		title("testGetEndingCloseBrace");
		setXmlBraces();
		Xml.setEndingCloseBrace("XXX");
		assertTrue(Xml.getEndingCloseBrace().equals("XXX"));
	}

//	public void testFindCloseStr() {
//		title("testFindCloseStr");
//		testFindCloseStr1();
//		testFindCloseStr2();
//		testFindCloseStr3();
//		testFindCloseStr4();
//		testFindCloseStr5();
//		testFindCloseStr6();
//		testFindCloseStr7();
//	}
//
//	public void testFindCloseStr1() {
//		title("testFindCloseStr1");
//		String	xml			= "<STU></STU>";
//		int		closeIdx	= Xml.findCloseStr("<STU>", "</STU>", xml);
//
//		println("xml      = " + xml);
//		println("closeIdx = " + closeIdx);
//		assertTrue(closeIdx == 5);
//	}
//
//	public void testFindCloseStr2() {
//		title("testFindCloseStr2");
//		String	xml			= "<STU>xxx</STU>";
//		int		closeIdx	= Xml.findCloseStr("<STU>", "</STU>", xml);
//
//		println("xml      = " + xml);
//		println("closeIdx = " + closeIdx);
//		assertTrue(closeIdx == 8);
//	}
//
//	public void testFindCloseStr3() {
//		title("testFindCloseStr3");
//		String	xml			= "<STU><STU></STU>";
//		int		closeIdx	= Xml.findCloseStr("<STU>", "</STU>", xml);
//
//		println("xml      = " + xml);
//		println("closeIdx = " + closeIdx);
//		assertTrue(closeIdx < 0);
//	}
//
//	public void testFindCloseStr4() {
//		title("testFindCloseStr3");
//		String	xml			= "</STU><STU></STU>";
//		int		closeIdx	= Xml.findCloseStr("<STU>", "</STU>", xml);
//
//		println("xml      = " + xml);
//		println("closeIdx = " + closeIdx);
//		assertTrue(closeIdx == 11);
//	}
//
//	public void testFindCloseStr5() {
//		title("testFindCloseStr5");
//		String	xml			= "<STU></STU><STU>";
//		int		closeIdx	= Xml.findCloseStr("<STU>", "</STU>", xml);
//
//		println("xml      = " + xml);
//		println("closeIdx = " + closeIdx);
//		assertTrue(closeIdx == 5);
//	}
//
//	public void testFindCloseStr6() {
//		title("testFindCloseStr6");
//		String	xml			= "<STU></STU></STU>";
//		int		closeIdx	= Xml.findCloseStr("<STU>", "</STU>", xml);
//
//		println("xml      = " + xml);
//		println("closeIdx = " + closeIdx);
//		assertTrue(closeIdx == 5);
//	}
//
//	public void testFindCloseStr7() {
//		title("testFindCloseStr5");
//		String	xml			= "<STU><STU></STU></STU>";
//		int		closeIdx	= Xml.findCloseStr("<STU>", "</STU>", xml);
//
//		println("xml      = " + xml);
//		println("closeIdx = " + closeIdx);
//		assertTrue(closeIdx == 16);
//	}

	public void testWritingNicely() {
		title("testWritingNicely");
		setXmlBraces();

		Xml.setPrintNice(true);
		String	xml = null;
		String	xml2 = "\n<myPrim>\n  <numFields>9\n  </numFields>\n  <myPrim>\n    <numFields>9\n    </numFields>\n    <myPrim>null\n    </myPrim>\n    <myPublicBoolean>true\n    </myPublicBoolean>\n    <myPublicByte>1\n    </myPublicByte>\n    <myPublicShort>1\n    </myPublicShort>\n    <myPublicInt>1\n    </myPublicInt>\n    <myPublicLong>1\n    </myPublicLong>\n    <myPublicChar>X\n    </myPublicChar>\n    <myPublicFloat>99.99\n    </myPublicFloat>\n    <myPublicDouble>999999.99\n    </myPublicDouble>\n  </myPrim>\n  <myPublicBoolean>true\n  </myPublicBoolean>\n  <myPublicByte>1\n  </myPublicByte>\n  <myPublicShort>1\n  </myPublicShort>\n  <myPublicInt>1\n  </myPublicInt>\n  <myPublicLong>1\n  </myPublicLong>\n  <myPublicChar>X\n  </myPublicChar>\n  <myPublicFloat>99.99\n  </myPublicFloat>\n  <myPublicDouble>999999.99\n  </myPublicDouble>\n</myPrim>";

		Prim	myPrim = new Prim();
		xml = Xml.write("myPrim", myPrim);

		strTest(xml,xml2);
	}

	public void testWritingNicelyAtomic() {
		title("testWritingNicelyAtomic");
		setXmlBraces();
		Xml.setPrintNice(true);

		String	xml = "\n<myAtom>999\n</myAtom>";
		String	xml2 = Xml.write("myAtom", 999);

		strTest(xml,xml2);

	}

	public void testReadAtomic() {
		title("testReadAtomic");
		setXmlBraces();
		Xml.setPrintNice(true);

		String	xml = "\n<myAtom>999\n</myAtom>";
		String	xml2 = null;
		Object	myAtom = null;

		try {
			myAtom = Xml.readObj(Integer.class, Xml.getTag("myAtom", xml));
		} catch (InstantiationException e) {
			e.printStackTrace();
			fail(e.getMessage());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			fail(e.getMessage());
		}

		xml2 = Xml.write("myAtom",myAtom);

		strTest(xml,xml2);

	}

	public void testReadNonAtomic() {
		title("testReadAtomic");
		setXmlBraces();
		Xml.setPrintNice(true);

		Prim	myPrim = new Prim();
		String	xml1 = Xml.write("myPrim", myPrim);
		Object	myAtom = null;

		println(toStr(xml1));

		try {
			myAtom = Xml.readObj(Prim.class, Xml.getTag("myPrim", xml1));
		} catch (InstantiationException e) {
			e.printStackTrace();
			fail("Could not instantiate an object");
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			fail("Illegal access while creating an object from xml");
		}

		println(toStr(myAtom));


	}

	public void	testGetFirstStr(String xml)	{
		fail("Not yet implemented");
	}
	public void testGetFirstTag(String xml) {
		fail("Not yet implemented");
	}
	public void testGetFirstXml(String xml) {
		fail("Not yet implemented");
	}

	public void testRemoveXml(String xml) {
		title("testRemoveXml");

		setXmlBraces();

		Xml.setPrintNice(true);
		String	xml1 = "<TAG1>aaaaaaa</TAG1><TAG2>bbbbbbb</TAG2><TAG2>bbbbbbb</TAG2><TAG2>bbbbbbb</TAG2><TAG3>ccccccc</TAG3>";
		String	xml2 = "<TAG1>aaaaaaa</TAG1><TAG2>bbbbbbb</TAG2><TAG2>bbbbbbb</TAG2><TAG3>ccccccc</TAG3>";

		strTest(Xml.removeTag("TAG1", xml1), xml2);
	}

}












